$(function() {
    var per = 1000;
    var score = 0;
    var timer = null;
    var time = 50;

    startGame();
    gameTimer();

    function gameTimer() {
        let timer = setInterval(() => {
            time--;
            $('#time').text("Time: " + time + " Seconds");
            if (time == 0 && score <= 15) {
                $('#win').text("您输了！");
                clearInterval(timer);
            } else if (time >= 0 && score >= 15) {
                $('#win').text("您赢了！");
                clearInterval(timer);
            }
        }, 1000);
    }

    function startGame() {
        if (score >= 13) {
            per = 700;
        }
        if (score >= 14) {
            per = 500;
        }
        if (score >= 15) {
            per = 300;
        }
        console.log(per);
        if (timer == null && time > 0) {
            let timer = setInterval(function() {
                let click = true;

                // 设置一个老鼠
                let rand = Math.ceil(Math.random() * 9);
                console.log(rand);
                let current = $("#mouse" + rand);
                current.attr('src', 'mouse.gif');
                current.on('click', function() {
                    if (click) {
                        score++;
                        click = false;
                    }
                    $("#score").text("Score: " + score);
                })
                clearInterval(timer);

                //取消当前设置的老鼠
                setTimeout(function() {
                    current.off('click');
                    current.attr('src', 'wait.gif');
                    current = null;
                    timer = null;
                    startGame();
                }, per);
            }, per);
        }
    }
});